package sk.tuke.android4all.dialer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnGenericMotionListener;
import android.view.ViewGroup;
import android.widget.TextView;
import sk.tuke.android4all.controls.CustomFragment;
import sk.tuke.android4all.listeners.SwipeListener;
import sk.tuke.android4all.services.Tts;

public class CallFragment extends CustomFragment {

	private static String TAG = "CallFragment";
	private Activity mActivity = null;
	private String selectedTag = null;
	int touchCounter = 0;
	private final int TOUCH_INTENSITY = 4;
	private SwipeListener mActionListener = null;
	private Tts mSpeechEngine;
	
	
	public CallFragment(Activity aActivity_in){
		mActivity = aActivity_in;
		selectedTag = "";
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_call, container, false);	       
        return rootView;
    }
	
	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setTouchListener();
        createUI();
    }
	
	private void setTouchListener() {
		mActionListener =  new SwipeListener(mActivity) {
            @Override
            public void onDoubleTapAction(int count) {
                Log.i("TAG", "DoubleTapped " + selectedTag);
                resetTouchCounter();
                if(selectedTag != null){
                	if(selectedTag == getResources().getString(R.string.fragmentdialer_contacts)){
                		mSpeechEngine.sayMessage(getResources().getString(R.string.fragmentcall_hanging));
                		hang();
                	}
                }	
            }

			@Override
			public void onZoomInAction() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onZoomOutAction() {
				// TODO Auto-generated method stub
				
			}};
	}
            
    private void hang(){
    	//Implement hang functionality
    }

	private void createUI() {
		final TextView aHangButton = (TextView) getView().findViewById(R.id.fragmentcall_hang);
		aHangButton.setOnGenericMotionListener(new OnGenericMotionListener(){

			@Override
			public boolean onGenericMotion(View arg0, MotionEvent arg1) {
				mActionListener.resetTouchCounter();
				String textInControl = aHangButton .getText().toString();
                if (selectedTag != textInControl && touchCounter>TOUCH_INTENSITY) {
                    touchCounter = 0;
                    mSpeechEngine.sayMessage(getResources().getString(R.id.fragmentcall_hang));
                    selectedTag = getResources().getString(R.id.fragmentcall_hang);
                }else{
                    touchCounter++;
                }
				return false;
			}});
	}

	@Override
	public void returnAction() {
		// TODO Auto-generated method stub
	}

}
